package io.renren.service.impl;

import io.renren.controller.AbstractController;
import io.renren.dao.*;
import io.renren.entity.*;
import io.renren.service.ActFlowService;
import io.renren.service.SysUploadService;
import io.renren.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.*;

import io.renren.service.LeaveService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;


@Service("leaveService")
public class LeaveServiceImpl extends AbstractController implements LeaveService {
	@Resource(name="leaveDao")
	private LeaveDao leaveDao;
	@Autowired
	private ActAssignDao actAssignDao;
	@Autowired
	private ActFlowService actFlowService;
	@Autowired
	private SysUploadService sysUploadService;

	@Override
	public LeaveEntity queryObject(Integer id){
		return leaveDao.queryObject(id);
	}
	
	@Override
	public List<LeaveEntity> queryList(Map<String, Object> map){
		return leaveDao.queryList(map);
	}
	
	@Override
	public int queryTotal(Map<String, Object> map){
		return leaveDao.queryTotal(map);
	}
	
	@Override
	public void save(LeaveEntity leave){
		leave.setFileId(String.valueOf(new Date().getTime()));
		leaveDao.save(leave);
	}

	@Override
	public void update(LeaveEntity leave){
		leaveDao.update(leave);
	}
	
	@Override
	public void delete(Integer id){
		leaveDao.delete(id);
	}
	
	@Override
	public void deleteBatch(Integer[] ids){
		for(int i=0;i<ids.length;i++) {
			LeaveEntity leaveEntity = queryObject(ids[i]);
			List<SysUploadEntity>	SysOssEntityList = sysUploadService.queryListByfileId(leaveEntity.getFileId());
			if(SysOssEntityList!=null&&SysOssEntityList.size()>0){
				for(SysUploadEntity list: SysOssEntityList){
					String url = list.getUrl();
					new File(url).delete();
				}
			}
			sysUploadService.deleteByfileId(leaveEntity.getFileId());
		}
		leaveDao.deleteBatch(ids);
	}

	@Override
	@Transactional
	public String submit(LeaveEntity leave){
		Double days=leave.getDays();//请假天数
/*
		判断请假天数在审批配置中哪条数据，首先查询是否属于某个区间
*/
         Map<String,Object> map=new HashMap();
         map.put("days",days);
		map.put("processName","spmc01");//数据字段中spmc01代表请假
		List<ActAssignEntity> listActAssign=actAssignDao.queryBYCondition(map);
		if(listActAssign==null||listActAssign.size()==0){
			return "请在审批配置中配置审批条件！";
		}
		ActAssignEntity actAssign=listActAssign.get(0);
		Map mp=actFlowService.submitFlow(actAssign,leave,"spmc01");
		if(mp.get("msg")!=null){
			String msg=mp.get("msg").toString();
			if(StringUtils.isNotBlank(msg)){
				return msg;
			}
		}
        leave=(LeaveEntity)mp.get("obj");
		leave.setSubmitDate(new Date());
		leave.setSubmitPerson(getUser().getUsername());
		leaveDao.update(leave);
		return "";
	}


/*

	String submitFlow(LeaveEntity leave){
		List<String> list=new ArrayList<String>();
		int k=0;//记录指定个人的次数
		String dept=getUser().getDeptNo();//当前部门
		String deptName=getUser().getDept();//当前部门名称
		String isSupervisor=getUser().getIsSupervisor();//申请人是否主管
		SysOfficeEntity office	=sysOfficeDao.queryObject(dept);//当前部门的数据
		String parentId=office.getParentId();//上级部门
		for(int i=0;i<actAssigndtlList.size();i++){
			ActAssigndtlEntity pojo=actAssigndtlList.get(i);
			String  type =pojo.getType();
			String username="";
			int level=pojo.getLevel()-k;
			if("0".equals(type)){//指定的是主管
*/
/*
				level为1取当前部门主管，level为2取上级主管，以此类推
*//*


				if("0".equals(isSupervisor)){
					//自己是主管,level为1取上级部门，level为2取上上级主管，以此类推
					level++;
				}

//				for(int j=2;j<=level;j++){
				if(level>1){
					parentId=office.getParentId();//上级部门
					office	=sysOfficeDao.queryObject(parentId);
					dept=office.getId()+"";
					deptName=office.getName();
				}
				//找出对应部门的主管
				List<SysUserEntity> listUser =sysUserDao.queryZgByDeptNo(dept);
				if(listUser==null||listUser.size()==0){
					return deptName+"没有主管，请先配置";
				}
				SysUserEntity user=listUser.get(0);
				username=user.getUsername();//主管姓名

			}else if("1".equals(type)){//指定是个人
				username=pojo.getUsername();
				k++;//如果第一级指定个人，第二级审批指定主管，此时指定的当前主管，而非上级主管

			}
			 if(StringUtils.isNoneBlank(username)){
				 list.add(username);
			 }
		}

		Collections.reverse( list);


		int nextIndex=0;
		int len=list.size();
//		int processLevel=0;
		int piid=(int)((Math.random()*9+1)*100000);
		leave.setPiid(piid);
		for (int i=0;i<len;i++){
			ActFlowEntity pojo=new ActFlowEntity();
			pojo.setPiid(piid);
			pojo.setProcessName("spmc01");
			pojo.setProcessLevel(len-i);
			pojo.setUsername(list.get(i));
			pojo.setNextIndex(nextIndex);
			actFlowDao.save(pojo);
			nextIndex=pojo.getId();
			leave.setNextUser(list.get(i));
		}
		leave.setPistatus("1");//一级主管审批
//		leave.setNextUser(list.get(0));
		leave.setNextTo(nextIndex);//保存第一个审批人的id
		leaveDao.update(leave);
         return "";
	}
*/





	@Override
	@Transactional
	public void process(LeaveEntity leave){
		Map<String, Object> map=new HashMap<>();
		map.put("piid",leave.getPiid());
		map.put("remark",leave.getRemark()==null?"":leave.getRemark());
		map.put("nextTo",leave.getNextTo());
		Object obj=actFlowService.saveFlow(map,leave,"agree","spmc01");
		leave=(LeaveEntity)obj;
		leaveDao.update(leave);

	}

	@Override
	@Transactional
	public void reject(LeaveEntity leave){

		Map<String, Object> map=new HashMap<>();
		map.put("piid",leave.getPiid());
		map.put("remark",leave.getRemark()==null?"":leave.getRemark());
		map.put("nextTo",leave.getNextTo());
		Object obj=actFlowService.saveFlow(map,leave,"reject","spmc01");
//		saveFlow(leave,"reject");
		leave=(LeaveEntity)obj;
		leave.setPistatus("10");//设置为拒绝的状态
		leaveDao.update(leave);


	}

/*
	void saveFlow(LeaveEntity leave1,String result){
		ActFlowEntity flow =actFlowDao.queryObject(leave.getNextTo());
		ActHistoryEntity pojo=new ActHistoryEntity();
		pojo.setPiid(leave.getPiid());
		pojo.setProcessName("leave");
		pojo.setProcessLevel(flow.getProcessLevel());
		pojo.setUsername(flow.getUsername());
		pojo.setApproveDate(new Date());
		pojo.setResult(result);
		pojo.setRemark(leave.getRemark());
		actHistoryDao.save(pojo);
		leave.setNextTo(flow.getNextIndex());
		if(flow.getNextIndex()==0){//等于0是最后一个人审批
			leave.setPistatus("0");
			leave.setNextUser("");

		}else{
			leave.setPistatus(String.valueOf(flow.getProcessLevel()+1));
			leave.setNextUser(actFlowDao.queryObject(flow.getNextIndex()).getUsername());


		}
		//流程审批完或者拒绝获取抄送人
		if(flow.getNextIndex()==0||"reject".equals(result)){
			List<ActSendtoEntity> listActSendto =actSendtoDao.queryObjectByProcess("spmc01");
			if(listActSendto!=null&&listActSendto.size()>0){
				leave.setNextUser(listActSendto.get(0).getUsername());
			}

		}
		flow.setApproveDate(new Date());
		actFlowDao.update(flow);
	}*/
}
